﻿using System;

namespace FreeGoo.GameEngine
{
    /// <summary>
    /// Copied from http://www.wiredprairie.us/blog/index.php/archives/723;
    /// Stopwatch is used to measure the general performance of Silverlight functionality. Silverlight
    /// does not provide a high resolution timer as is available in many operating systems,
    /// so the resolution of this timer is limited to "ticks." This class is best used to measure
    /// the relative performance of functions over many iterations.
    /// </summary>
    public sealed class Stopwatch
    {
        private int _startTick;
        private long _elapsed;
        private bool _isRunning;

        /// <summary>
        /// Creates a new instance of the class and starts the watch immediately.
        /// </summary>
        /// <returns>An instance of Stopwatch, running.</returns>
        public static Stopwatch StartNew()
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            return sw;
        }

        /// <summary>
        /// Creates an instance of the Stopwatch class.
        /// </summary>
        public Stopwatch()
        {
        }

        /// <summary>
        /// Completely resets and deactivates the timer.
        /// </summary>
        public void Reset()
        {
            _elapsed = 0;
            _isRunning = false;
            _startTick = 0;
        }

        /// <summary>
        /// Begins the timer.
        /// </summary>
        public void Start()
        {
            if (!_isRunning)
            {
                _startTick = Environment.TickCount;
                _isRunning = true;
            }
        }

        /// <summary>
        /// Stops the current timer.
        /// </summary>
        public void Stop()
        {
            if (_isRunning)
            {
                _elapsed += Environment.TickCount - _startTick;
                _isRunning = false;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the instance is currently recording.
        /// </summary>
        public bool IsRunning
        {
            get { return _isRunning; }
        }

        /// <summary>
        /// Gets the Elapsed time as a Timespan.
        /// </summary>
        public TimeSpan Elapsed
        {
            get { return new TimeSpan(GetCurrentElapsed()); }
        }

        /// <summary>
        /// Gets the Ellapsed time as the total number of milliseconds.
        /// </summary>
        public long EllapsedMilliseconds
        {
            get { return GetCurrentElapsed() / TimeSpan.TicksPerMillisecond; }
        }

        /// <summary>
        /// Gets the Ellapsed time as the total number of ticks
        /// </summary>
        public long EllapsedTicks
        {
            get { return GetCurrentElapsed(); }
        }

        private long GetCurrentElapsed()
        {
            return this._elapsed + (IsRunning ? (Environment.TickCount - _startTick) : 0);
        }
    }
}